﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ExternalOrgDO : EntityBase
    {
        /// <summary>
        /// 
        /// </summary>
        public ExternalOrgDO()
            : base(EntityType.Organizations, "ExternalOrgID", "OrgName")
        {
        }

        #region props
        private int _ExternalOrgID;
        /// <summary></summary>
        public int ExternalOrgID
        {
            get
            {
                return this._ExternalOrgID;
            }
            set
            {
                this._ExternalOrgID = value;
            }
        }

        private string _OrgName;
        /// <summary></summary>
        public string OrgName
        {
            get
            {
                return this._OrgName;
            }
            set
            {
                this._OrgName = value;
            }
        }

        private string _Department;
        /// <summary></summary>
        public string Department
        {
            get
            {
                return this._Department;
            }
            set
            {
                this._Department = value;
            }
        }

        private string _City;
        /// <summary></summary>
        public string City
        {
            get
            {
                return this._City;
            }
            set
            {
                this._City = value;
            }
        }

        private string _Region;
        /// <summary></summary>
        public string Region
        {
            get
            {
                return this._Region;
            }
            set
            {
                this._Region = value;
            }
        }

        private string _Country;
        /// <summary></summary>
        public string Country
        {
            get
            {
                return this._Country;
            }
            set
            {
                this._Country = value;
            }
        }

        private string _PostalCode;
        /// <summary></summary>
        public string PostalCode
        {
            get
            {
                return this._PostalCode;
            }
            set
            {
                this._PostalCode = value;
            }
        }

        private string _EIN;
        /// <summary></summary>
        public string EIN
        {
            get
            {
                return this._EIN;
            }
            set
            {
                this._EIN = value;
            }
        }

        private string _DunsID;
        /// <summary></summary>
        public string DunsID
        {
            get
            {
                return this._DunsID;
            }
            set
            {
                this._DunsID = value;
            }
        }
        #endregion

        #region Overrides of EntityBase


        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="entityNode"></param>
        public override void Save(ref XmlDocument xDoc, XmlNode entityNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Score", this.Score.ToString());
            XmlDataUtil.UpdateAttribute(
                ref xDoc, entityNode, "Flag", string.IsNullOrEmpty(this.Flag) ? "" : this.Flag.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "ExternalOrgID",this.ExternalOrgID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "OrgName", this.OrgName);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Department", this.Department);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "City", this.City);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Region", this.Region);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "Country", this.Country);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "PostalCode", this.PostalCode);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "EIN", this.EIN);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "DunsID", this.DunsID);
            XmlDataUtil.UpdateAttribute(ref xDoc, entityNode, "CollapsedID", this.CollapsedID.ToString());
        }
        #endregion

        public static List<EntityBase> ReadOrgs(XmlDocument xDoc)
        {
            List<EntityBase> orgs=new List<EntityBase>();
            XmlNodeList orgNodes = xDoc.DocumentElement.SelectNodes("//Org");
            if(orgNodes !=null && orgNodes.Count>0)
            {
                foreach(XmlNode orgNode in orgNodes)
                {
                    ExternalOrgDO org = ExternalOrgDO.Read(orgNode);
                    orgs.Add(org);
                }
            }
            return orgs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgNode"></param>
        /// <returns></returns>
        public static ExternalOrgDO Read(XmlNode orgNode)
        {
            ExternalOrgDO org=new ExternalOrgDO();
            org.Score = double.Parse(XmlDataUtil.GetAttributeValue(orgNode, "Score", "0"));
            org.Flag = XmlDataUtil.GetAttributeValue(orgNode, "Flag", "");
            org.ExternalOrgID = int.Parse(XmlDataUtil.GetAttributeValue(orgNode, "ExternalOrgID", "0"));
            org.OrgName = XmlDataUtil.GetAttributeValue(orgNode, "OrgName", "");
            org.Department = XmlDataUtil.GetAttributeValue(orgNode, "Department", "");
            org.City = XmlDataUtil.GetAttributeValue(orgNode, "City", "");
            org.Region = XmlDataUtil.GetAttributeValue(orgNode, "Region", "");
            org.Country = XmlDataUtil.GetAttributeValue(orgNode, "Country", "");
            org.PostalCode = XmlDataUtil.GetAttributeValue(orgNode, "PostalCode", "");
            org.EIN = XmlDataUtil.GetAttributeValue(orgNode, "EIN", "");
            org.DunsID = XmlDataUtil.GetAttributeValue(orgNode, "DunsID", "");
            org.CollapsedID = int.Parse(XmlDataUtil.GetAttributeValue(orgNode, "CollapsedID", "0"));
            return org;
        }
    }
}
